package com.shanxia.codec.cmd;

import com.shanxia.codec.ProtocolDataBase;
import com.shanxia.codec.enums.DecodeEnum;
import lombok.Data;

import java.util.Arrays;
import java.util.Objects;

import static com.shanxia.codec.Decoder.*;

/**
 * C -> S
 * 充电桩参数字符形设置/查询应答
 * 充电桩对后台服务器的充电桩字符形参数设置报文应答
 * CMD4
 */
@Data
public class Cmd4 extends ProtocolDataBase {

    public static final int MIN_SIZE = 42;

    private int v1;
    private int v2;
    private String sn;                        // 充电桩编码
    private int type;                         // 类型 [0-查询/ /1-设置]
    private int paramAddr;                    // 设置/查询参数起始地址
    private int code;                         // 设置/查询结果
    private String data;                      // 设置数据

    public Cmd4(){
        super.setCmd(4);
    }

    public static Cmd4 decodeFromBase(ProtocolDataBase protocolDataBase) {
        Cmd4 cmd = new Cmd4();
        cmd.copyDataFrom(protocolDataBase);
        byte[] dataField = protocolDataBase.getDataField();
        if (dataField.length < MIN_SIZE) {
            cmd.setResult(DecodeEnum.InvalidData);
            return cmd;
        }
        int offset = 0;
        cmd.v1 = getUint16(dataField, offset);
        offset += 2;
        cmd.v2 = getUint16(dataField, offset);
        offset += 2;
        // 32字节充电桩编码
        cmd.sn = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), true);
        offset += 32;
        // 1字节类型
        cmd.type = getUint8(dataField[offset++]);
        // 4字节设置/查询参数启始地址
        cmd.paramAddr = bytesToIntLittle(dataField, offset);
        offset += 4;
        // 1字节查询结果
        cmd.code = getUint8(dataField[offset++]);
        if (Objects.equals(cmd.type, 1)) {
            switch (cmd.paramAddr) {
                // 32字节充电桩编码
                case 1:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 32), true);
                    break;
                // 8字节标准时钟时间
                case 2:
                    cmd.data = getStandardTime(dataField, offset);
                    break;
                // 8字节管理员密码
                case 3:
                    // 8字节操作员密码
                case 4:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 8), true);
                    break;
                // 6字节MAC地址
                case 5:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 6), true);
                    break;
                // 16字节桩登入服务器密码  TODO 确认密码转换是否准确
                case 6:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 16), true);
                    offset += 16;
                    break;
                // 256桩主界面二维码
                case 7:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 256), true);
                    break;
                // 16字节客户服务热线 1
                case 8:
                    // 16字节客户服务热线 2
                case 9:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 16), true);
                    break;
                // 256用户支付二维码
                case 10:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 256), true);
                    break;
                // 128字节桩主界面二维码前缀
                case 11:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 128), true);
                    break;
                // 12字节DLT645-2007 电表地址
                case 12:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 12), true);
                    break;
                // 64字节云服务器域名
                case 13:
                    cmd.data = byteToAscii(Arrays.copyOfRange(dataField, offset, offset + 64), true);
                    break;
                default:
                    break;
            }
        }
        return cmd;
    }

    @Override
    public String toString() {

        StringBuffer buffer = new StringBuffer();
        buffer.append("#cmd4#").append("\n");
        buffer.append("v1:").append(v1).append("\n");
        buffer.append("v2:").append(v2).append("\n");
        buffer.append("充电桩编码:").append(sn).append("\n");
        buffer.append("类型 [0-查询/ /1-设置]:").append(type).append("\n");
        buffer.append("设置/查询参数起始地址:").append(paramAddr).append("\n");
        buffer.append("设置/查询 结果:").append(code).append("\n");
        buffer.append("设置数据 默认4个字节:").append(data);
        return buffer.toString();
    }
}
